<?php
namespace app\index\controller;
use app\index\model\CrmCustomer as ThisModel;
use app\index\validate\CrmCustomer as ThisValidate;
use think\Exception;

class Customer extends Base
{
    public $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = new ThisModel();
    }

    // 新增修改
    public function add()
    {
        try {
            $post = input();
            $validate = new ThisValidate();
            if ($validate->check($post)) {
                $post['jiafen_date'] = strtotime($post['jiafen_date']);
                if (isset($post['id'])) {
                    if ($this->table->save($post,['id'=>$post['id']])) {
                        apiReturn(200,'修改成功');
                    }else{
                        apiReturn(201,'修改失败');
                    }
                }else{
                    $post['manage_id'] = $this->username;
                    $post['date'] = time();
                    $max_customer_id = db('crm_customer')->max('customer_id');
                    $post['customer_id'] = (empty($max_customer_id)) ? 1 : ($max_customer_id+1);
                    if ($this->table->allowField(true)->save($post)) {
                        apiReturn(200,'新增成功');
                    }else{
                        apiReturn(201,'新增失败');
                    }
                }
            }else{
                apiReturn(201,$validate->getError());
            }
        } catch (Exception $e) {
            apiReturn(201,$e->getMessage());
        }
    }

    // 列表
    public function list()
    {
        $post = input();
        $limit = isset($post['rows']) ? $post['rows'] : 10;
        $page = isset($post['page']) ? $post['page'] : 1;
        $where = [];
        if ($this->is_xiaoshou == 1) {
            $where['manage_id'] = $this->getManageList();
        }
        // 组
        if (isset($post['zu_id'])) {
            if (!empty($post['zu_id'])) {
                $wherezu_id = $this->getFenzuList($post['zu_id']);
                $where['manage_id'] = $this->getManageList($wherezu_id);
            }
        }
        // 工号
        if (isset($post['manage_id'])) {
            if (!empty($post['manage_id'])) {
                $where['manage_id'] = array('in', $post['manage_id']);
            }
        }
        // 客户id
        if (isset($post['customer_id'])) {
            if (!empty($post['customer_id'])) {
                $where['customer_id'] = $post['customer_id'];
            }
        }
        // 客户姓名
        if (isset($post['name'])) {
            if (!empty($post['name'])) {
                $where['name'] = ['like','%'.$post['name'].'%'];
            }
        }
        // 客户电话
        if (isset($post['phone'])) {
            if (!empty($post['phone'])) {
                $where['phone'] = ['like','%'.$post['phone'].'%'];
            }
        }
        // 创建时间
        if (isset($post['starttime']) && isset($post['endtime'])) {
            if (!empty($post['starttime']) && !empty($post['endtime'])) {
                $where['date'] = array(array('egt',strtotime($post['starttime'])),array('elt',strtotime($post['endtime'])));
            }
        }
        // 加粉时间
        if (isset($post['jiafen_starttime']) && isset($post['jiafen_endtime'])) {
            if (!empty($post['jiafen_starttime']) && !empty($post['jiafen_endtime'])) {
                $where['jiafen_date'] = array(array('egt',strtotime($post['jiafen_starttime'])),array('elt',strtotime($post['jiafen_endtime'])));
            }
        }

        $list = $this->table->where($where)->order('id DESC')->page($page)->limit($limit)->select();
        foreach ($list as $key => &$value) {
            $value['manage_name'] = db('crm_manage')->where('username',$value['manage_id'])->value('name');
        }
        $count = $this->table->where($where)->count();
        apiReturn(200,'获取成功',['list'=>$list,'count'=>$count]);
    }

    // 删除
    public function delete()
    {
        $post = input();
        if (!isset($post['id'])) {
            apiReturn(201,'参数不完整');
        }
        if ($this->table->where('id',$post['id'])->delete()) {
            apiReturn(200,'删除成功');
        }else{
            apiReturn(201,'删除失败');
        }
    }

    // 信息
    public function info()
    {
        $post = input();
        if (!isset($post['customer_id'])) {
            apiReturn(201,'参数不完整');
        }
        $info = $this->table->where('customer_id',$post['customer_id'])->find();
        if (empty($info)) {
            apiReturn(201,'客户信息不存在');
        }
        apiReturn(200,'获取成功',$info);
    }

}
